package com.secsbrain.cmdb.biz.utils.command;

import org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;

/**
 * Created by yangyang.zhang on 2017/7/10.
 */
public class ParseYamlStrategy<T> extends ExecuteStrategyAdapter {
    private StringBuilder yamlString = new StringBuilder();
    private Class<T> resultClazz;
    private T result;

    public ParseYamlStrategy(Class<T> resultClazz) {
        this.resultClazz = resultClazz;
    }

    @Override
    public void processLine(String line) {
        if (StringUtils.contains(line, "----------")) {
            return;
        }
        yamlString.append(line);
        yamlString.append(System.lineSeparator());
    }

    @Override
    public void handleException(Exception e) {

    }

    @Override
    public void handleResult(Object result) {
        Yaml yaml = new Yaml();
        this.result = yaml.loadAs(yamlString.toString(), resultClazz);
    }

    public T getResult() {
        return result;
    }
}
